//
//  MeetingTimerView.swift
//  codelabs
//
//  Created by 雷雷 on 27/05/2025.
//

import SwiftUI

import ThemeKit
import TimerKit

struct MeetingTimerView: View {
    let speakers:[ScrumTimer.Speaker]
    
    let theme:Theme
    
    private var currentSpeakerName:String{
        speakers.first(where: {!$0.isCompleted})?.name ?? "Someone"
    }
    
    private var allDone:Bool{
        speakers.allSatisfy{
            $0.isCompleted
        }
    }
    
    var body: some View {
        Circle()
            .stroke(lineWidth: 24)
            .overlay{
                VStack {
                    
                    if allDone {
                        Text("DONE")
                            .font(.title)
                            .bold()
                    }else{
                        Text("\(currentSpeakerName)")
                            .font(.title)
                            .bold()
                        
                        Text("is speaking")
                            .font(.headline)
                            .foregroundStyle(.secondary)
                    }
                   
                }
                .accessibilityElement(children: .combine)
                .foregroundStyle(theme.accentColor)
            }
            .overlay{
                ForEach(speakers){speaker in
                    if speaker.isCompleted, let index = speakers.firstIndex(where:{$0.id == speaker.id}) {
                        SpeakerArc(
                            speakerIndex:index ,
                            totalSeakers: speakers.count
                        )
                        .rotation(Angle(degrees: -90))
                        .stroke(theme.mainColor,lineWidth: 12)
                    }
                }
            }
            .padding(.horizontal)
    }
}

#Preview(traits: .dailyScrumSampleData) {
    
    let speakers = [ScrumTimer.Speaker(name: "Bill", isCompleted: true), ScrumTimer.Speaker(name: "Cathy", isCompleted: false)]
    
    MeetingTimerView(speakers: speakers, theme: .orange)
    
}
